<template>
  <div class="footer">
    <div class="row">
      <a id="goodPoint" href="javascript:void(0);" @click="goodPoint" class="col-xs-6" :data-exist="hasPoint?'1':'0'" >
        <i class="iconfont icon-zan"></i>&nbsp;<span class="text">{{hasPoint?'已点赞':'赞'}}</span>(<span class="count">{{goodPointCount}}</span>)
      </a>

      <a id="subscribe" href="javascript:void(0);" @click="subscribe" class="col-xs-6" :data-exist="hasSub?'1':'0'">
        <i class="iconfont icon-dingyue"></i>&nbsp;<span class="text">{{hasSub?'已订阅':'订阅'}}</span><span
        class="count"></span>
      </a>
    </div>
  </div>
</template>

<script>
  import RemoteDAO from 'common/remoteDAO'
  import CommonUtil from 'common/common.util'
  import LocalDAO from 'common/localDAO'

  export default {
    data () {
      return {
        hasPoint: false,//是否点赞过
        hasSub: false,//是否订阅
        hasQueryPoint: false, //是否已查询到是否点赞
        hasQuerySub: false, //是否已查询到是否订阅,
        testData:'testData'
      }
    },
    props: {
      userCode: {
        type: String
      },
      clientId: {
        type: String
      },
      goodPointCount: {
        type: String
      }
    },
    created: function () {
      this.fetchData()
    },
    watch: {
      '$route.query.userCode' (to, from) {
        //跳转相同页面，组件不重新构建，无法调用created,故需监听
        this.fetchData()
      }
    },
    methods: {
      fetchData(){
        var self = this
        var relationUserCode = self.clientId;
        if (LocalDAO.getNickname()) {
          // 判断是否已点赞
          RemoteDAO.callUC12008309(self.userCode, "1", "1", "2", "1", "1", "", relationUserCode, "0", function (response) {
            if (response.error == "0") {
              if (response.data.body.length > 0) {
                self.hasPoint = true
              } else {
                self.hasPoint = false
              }
              self.hasQueryPoint = true
            }
          });

          // 判断是否已订阅
          RemoteDAO.callUC12008309(self.userCode, "1", "1", "3", "1", "1", "", relationUserCode, "0", function (response) {
            if (response.error == "0") {
              if (response.data.body.length > 0) {
                self.hasSub = true;
              } else {
                self.hasSub = false;
              }
              self.hasQuerySub = true
            } else {
              self.$store.dispatch('setTip', {text: "UC12008309:" + response.data});
            }
          });
        }
      },
      goodPoint(e){
        var self = this;
        // 埋点
        var object = {};
        object.usercode =　self.userCode;
        object.k2 = "";
        TDAPP.onEvent("2008", "", object);
        TDAPP.send();
        if (LocalDAO.getNickname()) {
          var exist = e.currentTarget.getAttribute("data-exist");
          if (!self.hasQueryPoint) { // 尚未获取到是否已点赞状态
            self.$store.dispatch('setTip', {text: "网速较慢，请稍后重试"});
          } else if (exist == "0") { // 未点赞
            var args = {};
            args.userCode = self.userCode;
            args.actionType = "00";
            args.businType = "NRNG";
            args.communityType = "2";
            args.objectType = "1";
            args.relationUserCode = self.clientId;
            args.relationUserName = "";
            args.beginDate = "";
            args.endDate = "";
            args.communityContent = "";
            args.topicCode = "";
            args.topicName = "";
            args.serialNo = "";
            RemoteDAO.callUC12008306(args, function (response) {
              if (Number(response.error) === 0) {
                if (response.data.body[0].error_no == "0") {
                  self.hasPoint = true;
                  var oldGoodpointCount = self.goodPointCount;
                  if (oldGoodpointCount != "") {
                    try {
                      self.goodPointCount = Number(oldGoodpointCount) + 1
                    } catch (error) {
                      console.log(error.getMessages());
                    }
                  }
                }
              } else {
                self.$store.dispatch('setTip', {text: response.data});
              }
            });
          } else if (exist == "1") { // 已点赞
            self.$store.dispatch('setTip', {text: "您已点赞该牛人！"});
          }
        } else {
          RemoteDAO.callNativeLogin(function (response) {
            if (response.error == "0") {
              self.$store.dispatch('setTip',{text: "已为您登录，请重新查看点击"});
              setTimeout(function () {
                window.location.reload(true);
              },1000)
            }
          });
        }
      },
      subscribe(e){
        var self = this;
        // 埋点
        var object = {};
        object.usercode = self.userCode;
        object.k2 = "";
        TDAPP.onEvent("2009", "", object);
        TDAPP.send();
        if (LocalDAO.getNickname()) {
          var exist = e.currentTarget.getAttribute("data-exist");
          if (!self.hasQuerySub) { // 尚未获取到是否已订阅状态
            self.$store.dispatch('setTip', {text: "网速较慢，请稍后重试"});
          } else if (exist == "0") { // 未订阅
            self.$store.dispatch('setModal', {visible: true, modalId: 'subscribeModal',relationUserCode:self.clientId,callback:function(response){
              self.hasSub = true;
            }});
          } else if (exist == "1") { // 已订阅
            self.$store.dispatch('setModal', {visible: true, modalId: 'subscribeCancelModal',relationUserCode:self.clientId,callback:function(response){
              self.hasSub = false;
            }});
          }
        } else {
          RemoteDAO.callNativeLogin(function (response) {
            if (response.error == "0") {
              self.$store.dispatch('setTip',{text: "已为您登录，请重新点击"});
              setTimeout(function () {
                window.location.reload(true);
              },500)
            }
          });
        }
      }
    }
  }
</script>

<style scoped>

</style>
